package com.ghsc.ghxc.jsb.ui.live.view.video.viewHolder

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.cn.appcore.ext.loge
import com.ghsc.ghxc.jsb.databinding.VideoListPlayItemBinding
import com.ghsc.ghxc.jsb.ui.live.bean.VideoDataBean
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.GSYVideoType

class VideoPlayPagerHolder(
    private val playerTag: String,
    private val itemBinding: VideoListPlayItemBinding,
) : RecyclerView.ViewHolder(itemBinding.root) {


    private var index = 0
    private var itemData: VideoDataBean? = null
    private var gsyVideoOptionBuilder: GSYVideoOptionBuilder? = null
    private var mVideoPlayListener: ((position: Int, dataInfo: VideoDataBean?) -> Unit)? = null

    fun setOnClickListener(listener: ((view: View?, position: Int) -> Unit)?) {
        itemBinding.videoItemPlayer.setOnClickListener(listener)
    }

    fun setOnVideoPlayListener(listener: ((position: Int, dataInfo: VideoDataBean?) -> Unit)?) {
        mVideoPlayListener = listener
    }

    init {
        gsyVideoOptionBuilder = GSYVideoOptionBuilder()
    }

    fun onBind(position: Int, data: VideoDataBean) {
        index = position
        itemData = data

        initPlayer(data)
    }

    private fun initPlayer(data: VideoDataBean) {
        loge("视频链接="+data.video)
        //防止错位，离开释放
//        gsyVideoPlayer.initUIState();
        gsyVideoOptionBuilder
            ?.setIsTouchWiget(false)
            ?.setUrl(data.video ?: "")
            ?.setVideoTitle(data.title ?: "")
            ?.setCacheWithPlay(false)
            ?.setLooping(true)
            ?.setRotateViewAuto(false)
            ?.setLockLand(false)
            ?.setPlayTag(playerTag)
            ?.setShowFullAnimation(true)
            ?.setNeedLockFull(true)
            ?.setPlayPosition(index)
            ?.setVideoAllCallBack(object : GSYSampleCallBack() {
                override fun onPrepared(url: String?, vararg objects: Any?) {
                    super.onPrepared(url, *objects)
                    mVideoPlayListener?.invoke(index, itemData)
//                    itemBinding.videoItemPlayer.seekTo(itemData?.currentPosition ?: 0)
                }
            })
            ?.build(itemBinding.videoItemPlayer)

        setPlayerDataInfo()

        val videoCover = data.image
        if (videoCover.isNullOrBlank()) {
            return
        }
        itemBinding.videoItemPlayer.loadCoverImage(videoCover)
    }

    fun startPlayer() {

        setPlayerDataInfo()
        setShowVideoType(itemData?.videoWidth, itemData?.videoHeight)
        itemBinding.videoItemPlayer.startPlayLogic()
    }
    fun pausePlayer() {
        itemBinding.videoItemPlayer.onVideoPause()
    }

    fun destroyPlayer(){
        itemBinding.videoItemPlayer.release()
    }

    fun setPlayerDataInfo() {
        itemBinding.videoItemPlayer.setVideoInfo(index, itemData)
    }

    private fun setShowVideoType(videoWidth: String?, videoHeight: String?) {
        val width = videoWidth?.toIntOrNull() ?: 0
        val height = videoHeight?.toIntOrNull() ?: 0
        val nowType = GSYVideoType.getShowType()
        val newType = if (width < height) {
            itemBinding.videoItemPlayer.setCoverImageScaleType(ImageView.ScaleType.FIT_XY)
            GSYVideoType.SCREEN_MATCH_FULL
        } else {
            itemBinding.videoItemPlayer.setCoverImageScaleType(ImageView.ScaleType.FIT_CENTER)
            GSYVideoType.SCREEN_TYPE_DEFAULT
        }
        if (nowType != newType) {
            GSYVideoType.setShowType(newType)
        }
    }
}